package com.lmqb.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lmqb.bean.SysMenu;
import com.lmqb.bean.SysRole;
import com.lmqb.bean.SysRolemenu;
import com.lmqb.bean.SysUser;
import com.lmqb.bean.SysUserOperationLog;
import com.lmqb.dao.SysMenuMapper;
import com.lmqb.dao.SysRoleMapper;
import com.lmqb.dao.SysRolemenuMapper;
import com.lmqb.dao.SysUserOperationLogMapper;
import com.lmqb.entry.MenuUsed;
import com.lmqb.entry.RoleInfo;
import com.lmqb.entry.UpdateRole;
import com.lmqb.service.RoleService;
import com.lmqb.util.StrUtil;

@Service("roleService")
public class RoleServiceImpl implements RoleService{

	@Autowired
	private SysRoleMapper sysRoleDao;
	
	@Autowired
	private SysMenuMapper sysMenuDao;
	
	@Autowired
	private SysRolemenuMapper roleMenuDao;
	
	@Autowired
	private SysUserOperationLogMapper sysUserLogDao;
	
	/*
	 * 获取所有角色列表
	 * */
	public List<SysRole> selectAll(SysRole sysRole) {
		List<SysRole> list=sysRoleDao.selectAll(sysRole);
		return list;
	}

	/* (non-Javadoc)
	 * @see com.lmqb.service.RoleService#insertRole(com.lmqb.entry.RoleInfo, com.lmqb.bean.SysUser)
	 */
	/*
	 * 添加角色。添加角色权限
	 * */
	public void insertRole(RoleInfo roleInfo,SysUser sysUser) {
		//添加角色。添加角色权限
		SysRole role=new SysRole();
		role.setRoleName(roleInfo.getRoleName());
		role.setRoleRemark(roleInfo.getRoleRemark());
		role.setRoleStatus((byte)1);
		sysRoleDao.insert(role);
		Integer[] menuIds=new Integer[roleInfo.getMenuList().size()];
		roleInfo.getMenuList().toArray(menuIds);
		List<SysMenu> list=sysMenuDao.selectMenus(menuIds);
		for(int i=0;i<list.size();i++){
			SysRolemenu roleMenu=new SysRolemenu();
			roleMenu.setRoleId(role.getRoleId());
			roleMenu.setMenuId(list.get(i).getMenuId());
			roleMenuDao.insert(roleMenu);
		}
		//添加管理员操作记录
		SysUserOperationLog log=new SysUserOperationLog();
		log.setAddTime(StrUtil.timeToInt(new Date()));
		log.setLoginName(sysUser.getLoginName());
		log.setObjectId(role.getRoleId()+"");
		log.setOperationCate((byte)3);
		log.setOperationContent("添加角色。"+role.toString()+"。拥有权限："+menuIds);
		log.setRealName(sysUser.getRealName());
		log.setUserId(sysUser.getUserId());
		log.setTableName("sys_role,sys_rolemenu");
		sysUserLogDao.insert(log);
	}

	//@Override
	public void update(SysRole sysRole) {
		sysRoleDao.updateByPrimaryKeySelective(sysRole);
		
	}

	//@Override
	public UpdateRole selectById(Integer roleId) {
		SysRole role=sysRoleDao.selectByPrimaryKey(roleId);
		UpdateRole updateRole=new UpdateRole();
		updateRole.setRoleId(roleId);
		updateRole.setRoleName(role.getRoleName());
		updateRole.setRoleRemark(role.getRoleRemark());
		SysMenu sysMenu=new SysMenu();
		sysMenu.setMenuStatus((byte)1);
		List<SysMenu> menuList=sysMenuDao.selectAll(sysMenu);
		List<SysRolemenu> list=roleMenuDao.selectByRoleId(roleId);
		List<MenuUsed> listUse=new ArrayList<MenuUsed>();
		for(int i=0;i<menuList.size();i++){
			MenuUsed menuUsed=new MenuUsed();
			menuUsed.setMenuId(menuList.get(i).getMenuId());
			menuUsed.setMenuName(menuList.get(i).getMenuName());
			menuUsed.setIsCheck(2);
			for(int j=0;j<list.size();j++){
				if(menuList.get(i).getMenuId()==list.get(j).getMenuId()){
					menuUsed.setIsCheck(1);
				}
			}
			listUse.add(menuUsed);
		}
		updateRole.setMenuList(listUse);
		return updateRole;
	}

	//@Override
	public void modifyRole(RoleInfo roleInfo,SysUser sysUser) {
		//修改角色信息
		SysRole role=new SysRole();
		role.setRoleId(roleInfo.getRoleId());
		role.setRoleName(roleInfo.getRoleName());
		role.setRoleRemark(roleInfo.getRoleRemark());
		sysRoleDao.updateByPrimaryKeySelective(role);
		//删除原来的角色权限
		roleMenuDao.deleteByRoleId(roleInfo.getRoleId());
		String str="";
		//添加新的角色权限
		for(int i=0;i<roleInfo.getMenuList().size();i++){
			SysRolemenu roleMenu=new SysRolemenu();
			roleMenu.setMenuId(roleInfo.getMenuList().get(i));
			roleMenu.setRoleId(roleInfo.getRoleId());
			roleMenuDao.insert(roleMenu);
			str+=roleInfo.getMenuList().get(i);
		}
		//添加管理员操作记录
		SysUserOperationLog log=new SysUserOperationLog();
		log.setAddTime(StrUtil.timeToInt(new Date()));
		log.setLoginName(sysUser.getLoginName());
		log.setObjectId(role.getRoleId()+"");
		log.setOperationCate((byte)3);
		log.setOperationContent("修改角色。"+role.toString()+"。拥有权限："+str);
		log.setRealName(sysUser.getRealName());
		log.setUserId(sysUser.getUserId());
		log.setTableName("sys_role,sys_rolemenu");
		sysUserLogDao.insert(log);
	}

	//@Override
	public SysRole selectByRoleId(Integer roleId) {
		SysRole role=sysRoleDao.selectByPrimaryKey(roleId);
		return role;
	}
}
